package com.koron.order.maintain.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.maintain.bean.entity.MaintainEquipmentPo;
import com.koron.order.maintain.bean.vo.MaintainEquipmentVo;
import com.koron.order.maintain.bean.vo.MaintainItemVo;
import com.koron.order.maintain.mapper.MaintainEquipmentMapper;
import com.koron.order.maintain.service.MaintainEquipmentService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * (MaintainEquipment)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-14 14:25:19
 */
@Service("maintainEquipmentService")
public class MaintainEquipmentServiceImpl implements MaintainEquipmentService {
    @Autowired
    HttpServletRequest httpServletRequest;

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, MaintainEquipmentPo maintainEquipment) {

        MaintainEquipmentMapper mapper = factory.getMapper(MaintainEquipmentMapper.class);

        return mapper.insert(maintainEquipment);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, MaintainEquipmentPo maintainEquipment) {
        MaintainEquipmentMapper mapper = factory.getMapper(MaintainEquipmentMapper.class);
        return mapper.update(maintainEquipment);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        MaintainEquipmentMapper mapper = factory.getMapper(MaintainEquipmentMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {

        MaintainEquipmentMapper mapper = factory.getMapper(MaintainEquipmentMapper.class);
        return mapper.deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryById")
    public MaintainEquipmentPo queryById(SessionFactory factory, String id) {

        MaintainEquipmentMapper mapper = factory.getMapper(MaintainEquipmentMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<MaintainEquipmentVo> queryByOrderId(SessionFactory factory, String orderId, String maintainEquipmentId) {

        MaintainEquipmentMapper mapper = factory.getMapper(MaintainEquipmentMapper.class);

        List<MaintainEquipmentVo> list = mapper.queryByOrderId(orderId,maintainEquipmentId);

        if (CollectionUtils.isNotEmpty(list)) {
            for (MaintainEquipmentVo maintainEquipmentVo : list) {
                List<MaintainItemVo> itemVos = maintainEquipmentVo.getItemVos();
                if (CollectionUtils.isNotEmpty(itemVos)) {
                    Map<String, MaintainItemVo> map = new HashMap<>();
                    for (MaintainItemVo itemVo : itemVos) {
                        String id = itemVo.getId();
                        String parentId = itemVo.getParentId();
                        itemVo.setMaintainItemVoList(new ArrayList<>());
                        if (StringUtils.isEmpty(parentId)) {
                            map.put(id, itemVo);
                        }
                    }

                    Iterator<MaintainItemVo> iterator = itemVos.iterator();

                    while (iterator.hasNext()) {
                        MaintainItemVo itemVo = iterator.next();
                        String id = itemVo.getId();
                        String parentId = itemVo.getParentId();
                        if (StringUtils.isNotEmpty(parentId)) {
                            MaintainItemVo maintainItemVo = map.get(parentId);
                            if (!Objects.isNull(maintainItemVo)) {
                                //找到了父节点,应该从原来列表删除
                                iterator.remove();
                                List<MaintainItemVo> maintainItemVoList = maintainItemVo.getMaintainItemVoList();
                                maintainItemVoList.add(itemVo);
                            }
                        }
                    }
                }
            }
        }

        return list;
    }

    @Override
    @TaskAnnotation("queryList")
    public List<MaintainEquipmentPo> queryList(SessionFactory factory, MaintainEquipmentPo maintainEquipment) {

        MaintainEquipmentMapper mapper = factory.getMapper(MaintainEquipmentMapper.class);

        List<MaintainEquipmentPo> list = mapper.queryList(maintainEquipment,httpServletRequest.getHeader("Language"));

        return list;
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<MaintainEquipmentPo> maintainEquipmentPoList) {
        if (CollectionUtils.isEmpty(maintainEquipmentPoList)) {
            return 0;
        }
        String maintainId = maintainEquipmentPoList.get(0).getMaintainId();
        MaintainEquipmentMapper mapper = factory.getMapper(MaintainEquipmentMapper.class);

        List<MaintainEquipmentPo> poList = mapper.queryByOrderIds(maintainId);
        if(CollectionUtils.isNotEmpty(poList)){
            mapper.deleteByIds(poList.stream().map(MaintainEquipmentPo::getId).collect(Collectors.joining(",")));
        }

        maintainEquipmentPoList.forEach(p -> {
            p.setId(CodeTools.getCode32());
        });

        return mapper.insertBatch(maintainEquipmentPoList);
    }

}